Author

Kailyn Kragas

Packages & Dataset

Code
library(tidyverse)
library(kableExtra)
library(gt)
aNames <- read_csv(here::here("Labs", 
                              "Lab09", 
                              "StateNames_A.csv"))

Interactive Table

Code
DT::datatable(aNames)

Part 1

Summarizing the Number of Allisons

Part 1

Code
aNames <- aNames %>%
  rename(Sex = Gender)

allisonsSummary <- aNames %>%
  filter(Name == "Allison") %>% 
  group_by(State, Sex) %>%
  summarise(Allisons = sum(Count)) %>%
  pivot_wider(names_from = Sex, values_from = Allisons) %>%
  replace_na(list(F = 0, M = 0))
Code
# https://discourse.gohugo.io/t/centre-align-r-markdown-kable-table/25810: align
# https://haozhu233.github.io/kableExtra/awesome_table_in_html.html: kable styling
allisonsSummary %>% 
  knitr::kable(format = "html", 
               col.names = c("State", 
                             "Number of Females",
                             "Number of Males"),
               align = 'crr', 
               caption = "Number of Allisons Born In Each State") %>%
  kable_styling(full_width = FALSE)
Number of Allisons Born In Each State
State Number of Females Number of Males
AK 232 0
AL 1535 0
AR 1198 0
AZ 1880 0
CA 12413 0
CO 1594 0
CT 1099 0
DC 321 0
DE 294 0
FL 4455 0
GA 3257 0
HI 183 0
IA 1477 0
ID 451 0
IL 5110 0
IN 3067 0
KS 1283 0
KY 1905 20
LA 1209 0
MA 2218 0
MD 2229 0
ME 340 0
MI 4014 0
MN 2374 0
MO 2882 0
MS 817 0
MT 226 0
NC 3435 0
ND 285 0
NE 807 0
NH 412 0
NJ 3052 0
NM 399 0
NV 729 0
NY 5747 0
OH 5487 0
OK 1421 0
OR 1186 0
PA 4307 0
RI 306 0
SC 1228 0
SD 376 0
TN 2488 0
TX 10192 0
UT 1125 0
VA 3220 0
VT 135 0
WA 1956 0
WI 2367 0
WV 813 0
WY 142 0

Part 2

Code
allison_f <- aNames  %>% filter(Name == "Allison",
                                Sex == "F")  

allison_Years <- allison_f %>% 
  group_by(Year) %>%
  summarise(Allisons = sum(Count))
Code
# https://gt.rstudio.com/reference/cols_align.html: align columns
allison_Years %>% 
  gt() %>%
  cols_label(Year = "Year Born", 
            Allisons = "Number of Babies Named Allison") %>%
  cols_align(align = "center", columns = everything()) %>%
  tab_header(title = md("**Number Of Babies Born Per Year**"))
Number Of Babies Born Per Year
Year Born Number of Babies Named Allison
1997 7274
1998 7861
1999 7023
2000 6314
2001 6209
2002 6237
2003 5850
2004 5871
2005 5631
2006 5560
2007 5450
2008 6237
2009 6579
2010 5856
2011 5453
2012 5411
2013 5422
2014 5440

Spelling by State

Part 1

Code
filters <- tibble(Name = c("Allan", "Alan", "Allen"))
als <- aNames %>% semi_join(filters, by = "Name")

al_Years <- als %>%
  group_by(Year, Name) %>%
  summarise(Als = sum(Count))
Code
# https://stackoverflow.com/questions/67750737/increase-caption-size-from-kableextra-table-in-rmarkdown: font_size
al_Years %>% 
  knitr::kable(format = "html", 
               col.names = c("Year Born", 
                             "Spelling",
                             "Number Of Babies Born"),
               align = 'c', 
               caption = "Allans, Alans, and Allens Born Each Year") %>%
  kable_styling(full_width = FALSE, 
                font_size = 14, 
                position = "center")
Allans, Alans, and Allens Born Each Year
Year Born Spelling Number Of Babies Born
1997 Alan 2155
1997 Allan 430
1997 Allen 1371
1998 Alan 2102
1998 Allan 455
1998 Allen 1289
1999 Alan 2220
1999 Allan 457
1999 Allen 1296
2000 Alan 2398
2000 Allan 435
2000 Allen 1319
2001 Alan 2622
2001 Allan 441
2001 Allen 1364
2002 Alan 2591
2002 Allan 443
2002 Allen 1365
2003 Alan 3083
2003 Allan 576
2003 Allen 1286
2004 Alan 3051
2004 Allan 561
2004 Allen 1273
2005 Alan 3189
2005 Allan 509
2005 Allen 1199
2006 Alan 3442
2006 Allan 473
2006 Allen 1201
2007 Alan 3225
2007 Allan 466
2007 Allen 1163
2008 Alan 3002
2008 Allan 436
2008 Allen 1120
2009 Alan 2817
2009 Allan 436
2009 Allen 1004
2010 Alan 2487
2010 Allan 363
2010 Allen 935
2011 Alan 2321
2011 Allan 299
2011 Allen 972
2012 Alan 2252
2012 Allan 287
2012 Allen 924
2013 Alan 2577
2013 Allan 328
2013 Allen 873
2014 Alan 2464
2014 Allan 282
2014 Allen 828

Part 2

Code
al_summary <- als %>% 
  filter(Year == 2000, State %in% c("CA", "PA")) %>%
  group_by(State, Name) %>%
  summarise(Als = sum(Count)) %>% 
  pivot_wider(names_from = Name, values_from = Als)
Code
al_summary %>%
  knitr::kable(format = "html", 
               align = "c",
               caption = "Allans, Alans, and Allens Born In CA & PA") %>%
  kable_styling(full_width = FALSE, 
                font_size = 10, 
                position = "center")
Allans, Alans, and Allens Born In CA & PA
State Alan Allan Allen
CA 584 131 176
PA 51 12 56

Part 3

Code
finalSummary <- al_summary %>%
  summarise(Total = sum(Allan, Alan, Allen),
            across(.cols = Alan:Allen, .f = ~ . / Total)) %>% 
  relocate(Total, .after = Allen)
Code
finalSummary %>%
  gt() %>%
  fmt_percent(columns = 2:3, decimals = 2) %>%
  cols_label(Allan = "Allan %",
             Alan = "Alan %",
             Allen = "Allen %",
             Total = "Total Number Of Babies Born") %>%
  cols_align(align = "center", columns = everything()) %>%
  tab_header(title = md("**Number Of Babies Born in CA & PA**"))
Number Of Babies Born in CA & PA
State Alan % Allan % Allen % Total Number Of Babies Born
CA 65.54% 14.70% 0.1975309 891
PA 42.86% 10.08% 0.4705882 119

Part 2

Code
# https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html#Table_Footnote: boostrap options
al_summary %>%
  knitr::kable(format = "html", 
               align = "crrr",
              caption = "Allans, Alans, and Allens Born In CA & PA") %>%
  kable_styling(full_width = T, 
                font_size = 12, 
                position = "center",
                bootstrap_options = "striped")
Allans, Alans, and Allens Born In CA & PA
State Alan Allan Allen
CA 584 131 176
PA 51 12 56

Part 3

Code
DT::datatable(aNames)